package com.fyzk.activity.zhifu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import com.fyzk.adapter.GenDanBounsAdapter;
import com.fyzk.base.BaseActivity;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

public class ShaiDanActivity extends BaseActivity {

    private Button mOpen;
//    private Button mNoOpen;
    private Button mSecret;
    private Button mBonus;
    private Button mProfit;
    private Button mChagrge;
    private TopTitleView chz_top_rl;
    private GridView mGv;
    private Button mBtnOk;
    private TextView mMoney;
    private CheckBox mCheckBox;
    private GenDanBounsAdapter mGenDanBounsAdapter;

    private int recommendType = -1; // 0推荐、1不推荐、2保密
    private int proportionType = -1;//1奖金分成，2利润分成，3投注彩金分成
    private int proportions = -1;//佣金百分比
    private int isShowAmt = -1;//是否显示购买金额：0显示；1不显示
    private int isShowMaxSP = -1;//是否显示此单最高赔率---0显示---1不显示

    private Long amount;
    private Button mJeShow;
    private Button mJeHide;
    private Button mSpShow;
    private Button mSpHide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shai_dan);
        Intent intent = getIntent();
        amount = intent.getLongExtra("amount", 0);

        recommendType = intent.getIntExtra("recommendType", 0);
        proportionType = intent.getIntExtra("proportionType", 1);
        proportions = intent.getIntExtra("proportions", -1);
        isShowAmt = intent.getIntExtra("isShowAmt", 0);
        isShowMaxSP = intent.getIntExtra("isShowMaxSP", 0);
        amount = amount / 100;
        initView();
    }

    private void initView() {
        mOpen = findViewById(R.id.tj_open);
//        mNoOpen = findViewById(R.id.tj_no_open);
        mSecret = findViewById(R.id.tj_secret);
        mBonus = findViewById(R.id.fc_bonus);
        mProfit = findViewById(R.id.fc_profit);
        mChagrge = findViewById(R.id.fc_charge);
        chz_top_rl = findViewById(R.id.chz_top_rl);
        mGv = findViewById(R.id.gridview);
        mBtnOk = findViewById(R.id.btn_ok);
        mMoney = findViewById(R.id.tv_money);
        mCheckBox = findViewById(R.id.checkbox);

        mJeShow = findViewById(R.id.je_show);
        mJeHide = findViewById(R.id.je_hide);
        mSpShow = findViewById(R.id.sp_show);
        mSpHide = findViewById(R.id.sp_hide);

        chz_top_rl.setOnTitleClickListener(this);
        mOpen.setOnClickListener(this);
//        mNoOpen.setOnClickListener(this);
        mSecret.setOnClickListener(this);
        mBonus.setOnClickListener(this);
        mProfit.setOnClickListener(this);
        mChagrge.setOnClickListener(this);
        mCheckBox.setOnClickListener(this);
        mBtnOk.setOnClickListener(this);
        mJeHide.setOnClickListener(this);
        mJeShow.setOnClickListener(this);
        mSpHide.setOnClickListener(this);
        mSpShow.setOnClickListener(this);

        mGenDanBounsAdapter = new GenDanBounsAdapter(this);
        mGv.setAdapter(mGenDanBounsAdapter);

        if (recommendType == 0) {
            mOpen.setSelected(true);
        } /*else if (recommendType == 1) {
            mNoOpen.setSelected(true);
        } */else if (recommendType == 2) {
            mSecret.setSelected(true);
        }

        if (proportions == 0) {
            mCheckBox.setChecked(true);
        } else {
            mGenDanBounsAdapter.setSelectPosition(proportions - 1);
        }

        if (proportionType == 1) {
            mBonus.setSelected(true);
        } else if (proportionType == 2) {
            mProfit.setSelected(true);
        } else if (proportionType == 3) {
            mChagrge.setSelected(true);
        }

        if (isShowAmt == 0) {
            mJeShow.setSelected(true);
        } else if (isShowAmt == 1) {
            mJeHide.setSelected(true);
        }

        if (isShowMaxSP == 0) {
            mSpShow.setSelected(true);
        } else if (isShowMaxSP == 1) {
            mSpHide.setSelected(true);
        }

        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (mCheckBox.isChecked()) {
                    mCheckBox.setChecked(false);
                }
                mGenDanBounsAdapter.setSelectPosition(position);
                proportions = position + 1;
            }
        });

        mMoney.setText("共" + amount + "米");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tj_open:
                mOpen.setSelected(true);
//                mNoOpen.setSelected(false);
                mSecret.setSelected(false);
                recommendType = 0;
                break;
//            case R.id.tj_no_open:
//                mNoOpen.setSelected(true);
//                mOpen.setSelected(false);
//                mSecret.setSelected(false);
//                recommendType = 1;
//                break;
            case R.id.tj_secret:
                mSecret.setSelected(true);
                mOpen.setSelected(false);
//                mNoOpen.setSelected(false);
                recommendType = 2;
                break;
            case R.id.fc_bonus:
                mBonus.setSelected(true);
                mProfit.setSelected(false);
                mChagrge.setSelected(false);
                proportionType = 1;
                break;
            case R.id.fc_profit:
                mProfit.setSelected(true);
                mBonus.setSelected(false);
                mChagrge.setSelected(false);
                proportionType = 2;
                break;
            case R.id.fc_charge:
                mChagrge.setSelected(true);
                mProfit.setSelected(false);
                mBonus.setSelected(false);
                proportionType = 3;
                break;
            case R.id.btn_ok:
                if (recommendType == -1) {
                    showToast("请选择推荐方式");
                } else if (proportionType == -1) {
                    showToast("请选择分成方式");
                } else if (proportions == -1) {
                    showToast("请选择佣金比例");
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("recommendType", recommendType);
                    intent.putExtra("proportions", proportions);
                    intent.putExtra("proportionType", proportionType);
                    intent.putExtra("isShowAmt", isShowAmt);
                    intent.putExtra("isShowMaxSP", isShowMaxSP);
                    setResult(IntentParamUtils.OK, intent);
                    finish();
                }
                break;
            case R.id.checkbox:
                mGenDanBounsAdapter.setSelectPosition(-1);
                if (mCheckBox.isChecked()) {
                    proportions = 0;
                } else {
                    proportions = -1;
                }
                break;
            case R.id.je_hide:
                mJeHide.setSelected(true);
                mJeShow.setSelected(false);
                isShowAmt = 1;
                break;
            case R.id.je_show:
                mJeHide.setSelected(false);
                mJeShow.setSelected(true);
                isShowAmt = 0;
                break;
            case R.id.sp_hide:
                mSpHide.setSelected(true);
                mSpShow.setSelected(false);
                isShowMaxSP = 1;
                break;
            case R.id.sp_show:
                mSpHide.setSelected(false);
                mSpShow.setSelected(true);
                isShowMaxSP = 0;
                break;
        }
    }

}
